package com.ishansong.gis.tool;

import com.mongodb.ServerAddress;

import java.util.Arrays;
import java.util.List;

/**
 * Created by danqiang on 2017/7/24.
 * 参数解析
 */

@SuppressWarnings("all")
class Commander {
    static class ArgsHolder {
        int threadNum = 4;
        List<ServerAddress> oldSeeds = null;
        List<ServerAddress> newSeeds = null;

        enum Task {
            PERF_COUNT,
            PERF_READ,
            PERF_WRITE,
            IMPORT
        }

        Task task = Task.IMPORT;
        boolean verbose = false;
    }

    private void exit() {
        System.out.println("Sample: java -cp './lib/*' com.ishansong.gis.tool.Do -j 4 -p [mac|dev|local|sstable|fstable|stable|prod] -t [import|read|write|count] ");
        System.out.println("Default -p is mac, -t is import");
        System.exit(0);
    }

    ArgsHolder parse(String[] args) {
        int threadNum;
        if (args.length < 2 || !args[0].equals("-j")) {
            exit();
        }
        threadNum = Integer.parseInt(args[1]);
        if (threadNum <= 0) {
            exit();
        }
        ArgsHolder argsHolder = new ArgsHolder();
        argsHolder.threadNum = threadNum;

        argsHolder.oldSeeds = Arrays.asList(
                new ServerAddress("localhost", 27000)
        );
        argsHolder.newSeeds = Arrays.asList(
                new ServerAddress("localhost", 6300),
                new ServerAddress("localhost", 6301)
//                new ServerAddress("localhost", 27017)
        );

        if (args.length >= 4 && args[2].equals("-p")) {
            if (args[3].equals("dev")) {
                argsHolder.oldSeeds = Arrays.asList(
                        new ServerAddress("mt2", 27017)
                );
                argsHolder.newSeeds = Arrays.asList(
                        new ServerAddress("mt1", 27017),
                        new ServerAddress("mt3", 27017)
                );
            } else if (args[3].equals("prod")) {
                argsHolder.oldSeeds = Arrays.asList(
                        new ServerAddress("10.170.255.116", 27017)//10.170.253.91:27017,10.170.255.116:27017
                );
                argsHolder.newSeeds = Arrays.asList(//10.144.112.245:27017,10.80.52.74:27017,10.144.112.169:27017
                        new ServerAddress("10.144.112.245", 27017),
                        new ServerAddress("10.80.52.74", 27017),
                        new ServerAddress("10.144.112.169", 27017)
                );
            } else if (args[3].equals("fstable")) {
                argsHolder.oldSeeds = Arrays.asList(
                        new ServerAddress("10.29.172.124", 27017)
                );
                argsHolder.newSeeds = Arrays.asList(
                        new ServerAddress("10.29.172.124", 27017)
                );
            } else if (args[3].equals("sstable")) {
                argsHolder.oldSeeds = Arrays.asList(
                        new ServerAddress("10.30.59.24", 27017)
                );
                argsHolder.newSeeds = Arrays.asList(
                        new ServerAddress("10.30.59.24", 27017)
                );
            } else if (args[3].equals("stable")) {
                argsHolder.oldSeeds = Arrays.asList(
                        new ServerAddress("10.171.15.136", 27017)
                );
                argsHolder.newSeeds = Arrays.asList(
                        new ServerAddress("10.171.15.136", 27017)
                );
            } else if (args[3].equals("local")) {
                argsHolder.oldSeeds = Arrays.asList(
                        new ServerAddress("10.1.0.7", 27017)
                );
                argsHolder.newSeeds = Arrays.asList(
                        new ServerAddress("10.1.0.7", 27017)
                );
            }
        }

        argsHolder.task = ArgsHolder.Task.IMPORT;
        if (args.length >= 6 && args[4].equals("-t")) {
            if (args[5].equals("read")) {
                argsHolder.task = ArgsHolder.Task.PERF_READ;
            } else if (args[5].equals("write")) {
                argsHolder.task = ArgsHolder.Task.PERF_WRITE;
            } else if (args[5].equals("count")) {
                argsHolder.task = ArgsHolder.Task.PERF_COUNT;
            }
        }

        if (args.length >= 7 && args[6].equals("-v")) {
            argsHolder.verbose = true;
        }

        return argsHolder;
    }

}
